<?php

namespace common\service;

use common\model\StatusModel;


class TaskOutSourceService
{

    /**
     * 判断是否外包订单,任务是否开始制作
     * @param $data
     * @param $code
     * @param $entityData
     * @return array|mixed
     */
    public function outTaskStatus($data, $code, $entityData)
    {
        if (empty($data)) {
            return [];
        }
        // 处理没有开始的任务状态
        $statusList = model(StatusModel::class)->select();

        $needArr = [];
        foreach ($statusList as $val) {
            if ($val['correspond'] == 'not_started') {
                $needArr[] = $val['id'];
            }
        }
        $taskStartData = [];
        $outSourceNewData = []; // 要判断是否外包的数据
        foreach ($entityData as $oVal) {
            if (!empty($oVal['executor_tenant_id']) && $oVal['tenant_id'] != $oVal['executor_tenant_id']) {
                $outSourceNewData[] = $oVal['code'];
            }
            $taskId = json_decode($oVal['json'], true)['task_status'] ?? '';
            // 如果有一条任务已经开始制作,就认为该任务已经开始了
            if (in_array($taskId, $needArr)) {
                continue;
            }
            $taskStartData[] = $oVal['code'];
        }
        foreach ($data as &$val) {
            $val['is_outsourcing'] = false; // 是否外包
            $val['task_is_start'] = false; // 任务是否开始
            if (!empty($val[$code]) && in_array($val[$code], $outSourceNewData)) {
                $val['is_outsourcing'] = true;
            }
            if (!empty($val[$code]) && in_array($val[$code], $taskStartData)) {
                $val['task_is_start'] = true;
            }
        }
        return $data;
    }

    /**
     * 处理序列下任务是否外包,是否有开始任务
     * @param $data
     * @param $entityData
     * @return mixed
     */
    public function seqTaskStatus($data, $entityData)
    {
        // 外包的数据
        $outSourceNewData = [];
        foreach ($entityData as $key => $oVal) {
            foreach ($oVal as $value) {
                if (empty($value['executor_tenant_id']) || ($value['tenant_id'] == $value['executor_tenant_id'])) {
                    continue;
                }
                $outSourceNewData[$key][] = $value;
            }
        }
        // 有开始的任务状态
        $needArr = [];
        $taskStartData = [];
        $statusList = model(StatusModel::class)->select();
        foreach ($statusList as $val) {
            if ($val['correspond'] == 'not_started') {
                $needArr[] = $val['id'];
            }
        }
        foreach ($entityData as $key => $oVal) {
            foreach ($oVal as $value) {
                $taskId = json_decode($value['json'], true)['task_status'] ?? '';
                if (in_array($taskId, $needArr)) {
                    continue;
                }
                $taskStartData[$key][] = $value;
            }
        }
        foreach ($data as &$val) {
            $val['is_outsourcing'] = false; // 是否外包
            $val['task_is_start'] = false; // 任务是否开始
            if (!empty($val['id']) && !empty($outSourceNewData[$val['id']])) {
                $val['is_outsourcing'] = true;
            }
            if (!empty($val['id']) && !empty($taskStartData[$val['id']])) {
                $val['task_is_start'] = true;
            }
        }
        return $data;
    }

}